package com.coocaa.optimizer.movie.cleaner;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Log;

import com.umeng.analytics.MobclickAgent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

public class BackgroundActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }


    private static final String ACTION_CLEAN_NOW = "com.coocaa.cleaner.CleanNow";

    @Override
    protected void onResume() {
        super.onResume();
        try {
//            Intent i = getIntent();
//            if (i != null) {
//                String action = i.getAction();
//                if (!TextUtils.isEmpty(action) && ACTION_CLEAN_NOW.equals(action))
                    CleanerService.startScanAndAutoClean(getApplicationContext());
//            }
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            submitProviderLog(this);
            MobclickAgent.onResume(this);
            MobclickAgent.onPageStart("BackgroundActivity");
        } catch (Exception e) {
            e.printStackTrace();
        }
        finish();
    }

    private void submitProviderLog(Context context){
        String coocaaVersion = "";
        try {
            coocaaVersion = getCoocaaVersionString();
        }catch (Exception e){
            e.printStackTrace();
            coocaaVersion = "";
        }
        try {
            String skymid = SystemProperties.get("ro.build.skymid");
            String skymodel = SystemProperties.get("ro.build.skymodel");
            String skytype = SystemProperties.get("ro.build.skytype");
            Map<String,String> map = new HashMap<String,String>();
            map.put("skymid",skymid);
            map.put("skymodel-skytype",skymodel+"-"+skytype);
            map.put("coocaaVersion",coocaaVersion);
            Log.d("aclean","55submitCount skymodel:"+skymodel+" skytype:"+skytype+" coocaaVersion:"+coocaaVersion);
            MobclickAgent.onEvent(context, "cleaner",map);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static String getCoocaaVersionString() throws Exception {
        String version = "";
        try {
            String path = Environment.getRootDirectory() + "/vendor/TianciVersion";
            if (new File(path).exists()) {
                StringBuilder sb = new StringBuilder();
                String s = "";
                BufferedReader br = new BufferedReader(new FileReader(new File(path)));
                if ((s = br.readLine()) != null) {
                    sb.append(s + "\n");
                }
                br.close();
                version = s;
            } else {
                throw new Exception("There has no /vendor/TianciVersion");
            }
        } catch (Exception e) {
            throw new Exception("There has no /vendor/TianciVersion");
        }
        return version;
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
            MobclickAgent.onPause(this);
            MobclickAgent.onPageEnd("BackgroundActivity");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
